package com.yunmang.fragment;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnItemClickListener;
import com.google.gson.Gson;
import com.yunmang.R;
import com.yunmang.app.AppConstant;
import com.yunmang.entity.BatchDetailBean;
import com.yunmang.entity.NameBean;
import com.yunmang.entity.ParameterEntity.EditBatchBean;
import com.yunmang.http.HttpUtils;
import com.yunmang.utils.CommonUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import rx.Subscriber;

/**
 * Created by Administrator on 2017/5/26.
 */
public class PiciFragment2 extends BaseFragment implements OnItemClickListener {
    @InjectView(R.id.iv_exc_num)
    ImageView ivExcNum;
    @InjectView(R.id.et_card)
    EditText etCard;
    @InjectView(R.id.iv_right_exc)
    ImageView ivRightExc;
    @InjectView(R.id.et_driver_name)
    EditText etDriverName;
    @InjectView(R.id.rl_exception_category)
    RelativeLayout rlExceptionCategory;
    @InjectView(R.id.iv_right)
    ImageView ivRight;
    @InjectView(R.id.et_driver_phone)
    EditText etDriverPhone;
    @InjectView(R.id.rl_exception_accident)
    RelativeLayout rlExceptionAccident;
    @InjectView(R.id.iv_exc_person)
    ImageView ivExcPerson;
    @InjectView(R.id.et_car_xianfu)
    EditText etCarXianfu;
    @InjectView(R.id.iv_dache)
    ImageView ivDache;
    @InjectView(R.id.et_car_daofu)
    EditText etCarDaofu;
    @InjectView(R.id.iv_zxf)
    ImageView ivZxf;
    @InjectView(R.id.et_zxf)
    EditText etZxf;
    @InjectView(R.id.iv_right_reason)
    ImageView ivRightReason;
    @InjectView(R.id.et_notes)
    EditText etNotes;
    @InjectView(R.id.btn_add)
    Button btnAdd;
    private Subscriber<BatchDetailBean> subscriber;
    private Subscriber<NameBean> mSubScriber;
    private BatchDetailBean mbatchDetailBean;
    private String id;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_batch_plate;
    }

    @Override
    public void initView(View view) {
        ButterKnife.inject(this, view);
    }

    @Override
    public void initData() {
        id = getActivity().getIntent().getStringExtra("batchid");
        requestData(id);
    }


    private void requestData(String bathchId) {
        showWaitingDialog("数据加载中……");
        subscriber = new Subscriber<BatchDetailBean>() {
            @Override
            public void onCompleted() {
                hideWaitingDialog();
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                showToast("获取异常");
            }

            @Override
            public void onNext(BatchDetailBean batchDetailBean) {
                if (batchDetailBean.getCode() == 200) {
                    mbatchDetailBean = batchDetailBean;
                    fillUI(batchDetailBean);
                } else {
                    showToast("获取失败");
                }
            }
        };
        HttpUtils.getInstance().getBatchDetailById(subscriber, AppConstant.userid, Integer.parseInt(bathchId));
    }

    private void fillUI(BatchDetailBean batchDetailBean) {
        etCard.setText(batchDetailBean.getModel().getPlate());
        etDriverName.setText(batchDetailBean.getModel().getDriverName());
        etDriverPhone.setText(batchDetailBean.getModel().getDriverPhone());
        etCarXianfu.setText(batchDetailBean.getModel().getCarCostByShipper() + "");
        etCarDaofu.setText(batchDetailBean.getModel().getCarCostByConsignee() + "");
        etZxf.setText(batchDetailBean.getModel().getStevedoreCost() + "");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }


    @OnClick(R.id.btn_add)
    public void onClick() {
        if(mbatchDetailBean.getModel().isCanEdit()){
            editInfo(constructData());
        }else{
            showAlertDialog("已经交接不能修改！");
        }

    }

    /**
     * 弹出对话框
     * @param string
     * */
    private void showAlertDialog(String string) {
        new AlertView("提示",string, null,new String[]{"确定"}, null,getActivity(),AlertView.Style.Alert,this).show();
    }

    private void editInfo(String s) {
        showWaitingDialog("数据提交中");
        mSubScriber=new Subscriber<NameBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                showToast("提交异常");
                hideWaitingDialog();
            }

            @Override
            public void onNext(NameBean nameBean) {
                hideWaitingDialog();
                if(nameBean.getCode()==200){
                    showToast("提交成功");
                }else{
                    showToast("提交失败");
                }
            }
        };
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), s);
        HttpUtils.getInstance().editBatch(mSubScriber,body);
    }

    private String constructData() {
        Gson gson=new Gson();
        EditBatchBean bean=new EditBatchBean();
        bean.setSendDate(mbatchDetailBean.getModel().getSendDate());
        bean.setId(mbatchDetailBean.getModel().getId());
        bean.setNote(CommonUtils.getTextViewValue(etNotes));
        EditBatchBean.CarBean car=new EditBatchBean.CarBean();
        car.setId(mbatchDetailBean.getModel().getCarId());
        car.setDriverPhone(CommonUtils.getTextViewValue(etDriverPhone));
        car.setDriverName(CommonUtils.getTextViewValue(etDriverName));
        car.setPlate(CommonUtils.getTextViewValue(etCard));
        bean.setCar(car);
        bean.setStevedoreCost(Double.valueOf(CommonUtils.getTextViewValue(etZxf)));
        bean.setCarCostByConsignee(Double.valueOf(CommonUtils.getTextViewValue(etCarDaofu)));
        bean.setCarCostByShipper(Double.valueOf(CommonUtils.getTextViewValue(etCarXianfu)));
        bean.setUserId(AppConstant.userid);
        return gson.toJson(bean);
    }

    @Override
    public void onItemClick(Object o, int position) {

    }
}
